package server;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import data.Data;


public class GooglePageRank extends Thread{
	DBObject domain;
	public GooglePageRank(DBObject domain)
	{
		System.out.println(domain);
		this.domain=domain;
		this.start();
	}
	public void run()
	{
		String message = null;
		try {
			message = URLEncoder.encode(domain.get("name").toString(), "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}


        try {
        	
        	URL url = new URL("http://gpagerank.net/pr.jsp?url=http://"+message+"&f=text");
        	HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setReadTimeout(500000);
    		connection.setConnectTimeout(1000000);
    		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("iso-8859-1")));
    		String content = "";
	       	 while(1==1)
				 {
					 String str = reader.readLine();
					 if(str==null) break;
					 content+=str;   				
				 }
	       	
	       	 int googlePageRank = 0;
	       	 if(content != "")
	       	 {
	       		 googlePageRank = Integer.parseInt(content);
	       	 }
	    	 
			
//			 DBCollection coll1 = Data.data("domain");
//			 BasicDBObject info= new BasicDBObject();
			 domain.put("googlePagerank",googlePageRank);
//			 BasicDBObject set = new BasicDBObject("$set", info);
//			 coll1.update(domain, set);	
			 
			 url = new URL("http://api.mywot.com/0.4/public_query2?url=http://"+domain.get("name"));

			connection = (HttpURLConnection) url.openConnection();
			
			connection.setReadTimeout(500000);
			connection.setConnectTimeout(1000000);
			
			reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
			content = "";
			while(1==1)
			{
				String str = reader.readLine();
				if(str==null) break;
				content+=str;
				
			}
			
			Document doc = Jsoup.parse(content);
			Elements checks = doc.select("application");
			 
			if(checks.size() > 0 && checks.size() == 4)
			{
				int check = Integer.parseInt(doc.select("application").last().attr("r").trim());
				domain.put("wot", check);
			}
			
        } catch (MalformedURLException e) {
            // ...
        } catch (IOException e) {
            e.printStackTrace();
        }
	}
//	public static void main( String args[])
//	{
//		DBCollection coll1 = Data.data("domain");
//		DBObject domain = 	coll1.findOne(new BasicDBObject("name","google.com.vn"));
//		
//		new GooglePageRank(domain);
//	}
}
